#include "dominion.h"
#include "dominion_helpers.h"
#include "rngs.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

#define max( a, b ) ( ((a) > (b)) ? (a) : (b) )

// Random testing for the Adventurer Card (enum 7)
int main()
{
	int k[10] = {adventurer, gardens, embargo, village, minion, mine, cutpurse, sea_hag, tribute, smithy};
	struct gameState G;
	int randomSeed;
	int m;
	int numPlayer = 2;
	int i, j;
	int num_tests = 5000;
	int deck_count = 75;		// deck_, discard_, and hand_count set to maxes
	int discard_count = 50;
	int hand_count = 10;
	int total_count = deck_count + discard_count + hand_count;
	int tcount;
	
	int card_1;			// Where we hide the first treasure
	int card_2;			// Where we hide the second treasure
	
	srand(time(NULL));
	
	
	int t_deck[deck_count];
	int t_discard[discard_count];
	int t_hand[hand_count];
	
	for(i=0;i<deck_count;i++)
		t_deck[i] = 1; 
	for(i=0;i<discard_count;i++)
		t_discard[i] = 1;
	for(i=0;i<hand_count;i++)
		t_hand[i] = 1;
	
	

	randomSeed = (rand() % 10)+1;
	
	m = initializeGame(numPlayer, k, randomSeed, &G);
	
	// We randomly hide 2 treasure cards between hand, discard, and deck.
	for(i=0;i<num_tests;i++)
	{
		deck_count = rand() % 10;
		hand_count = rand() % 10 + 1; 	//theoretically, it's impossible to play an adventurer with no hand!
		discard_count = rand() % 10;
		total_count = deck_count + hand_count + discard_count;
		printf("Checking with %i deck, %i hand, %i discard. ", deck_count, hand_count, discard_count);
		
		memcpy(G.hand[0], t_hand, sizeof(int) * hand_count);
		G.handCount[0] = hand_count;
		memcpy(G.discard[0], t_discard, sizeof(int) * discard_count);
		G.discardCount[0] = discard_count;
		memcpy(G.deck[0], t_deck, sizeof(int) * deck_count);
		G.deckCount[0] = deck_count;
		card_1 = (rand() % total_count);
		card_2 = (rand() % total_count);		// if these are the same value, only one treasure placed
	
		// Hide the cards
		if(card_1 < hand_count)
			G.hand[0][card_1] = 6;
		else if(card_1 < hand_count + deck_count)
			G.deck[0][card_1 - hand_count] = 6;
		else
			G.discard[0][card_1 - hand_count - deck_count] = 6;
		
		if(card_2 < hand_count)
			G.hand[0][card_2] = 6;
		else if(card_2 < hand_count + deck_count)
			G.deck[0][card_2 - hand_count] = 6;
		else
			G.discard[0][card_2 - hand_count - deck_count] = 6;
		
		// Play the card
		cardEffect(7, 0, 0, 0, &G, 0, 0);

		// Check hand for treasure cards
		for(j=0;j<G.handCount[0];j++)
		{	
			if(G.hand[0][j] == 6)
				tcount++;
		}
		if(tcount == 2)
			printf("PASSED.");
		else if(card_1 == card_2 && tcount == 1)
			printf("PASSED.");
		else
		{	
			printf("FAILED.");
			exit(0);
		}
		printf("\n- ");
		tcount = 0;
	}
	return 0;
}